home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: nanninr@ix.netcom.com(Robert A. Nannini )
- Newsgroups: comp.lang.c++
- Subject: Re: Using BC4.5 TArrayAsVector of my class in a class!
- Date: 4 Jan 1996 16:24:55 GMT
- Organization: Netcom
- Message-ID: <4cgv0n$djd@ixnews8.ix.netcom.com>
- References: <DKMHJE.B0B@westminster.ac.uk>
- NNTP-Posting-Host: ix-stl4-18.ix.netcom.com
- X-NETCOM-Date: Thu Jan 04 8:24:55 AM PST 1996
-
- In <DKMHJE.B0B@westminster.ac.uk> besec@wmin.ac.uk (Nicholas Arthur
- Llewellyn) writes:
- >
- >This is my first use of BC4.5 and I'm trying to use the array class
- >library to hold the class Card, in the class's Pack and Hand.
- >
- >I get the error:
- >
- >Compiling PROJECT.CPP:
- >
- ><This is the one I'm trying to fix>
- >Error PROJECT.CPP 71: Cannot find default constructor to initialize
- >member 'pack' in function Pack::Pack()
- >
- ><These other two I got from messing around trying to fix the above
- >error>
- >Error PROJECT.CPP 72: Type qualifier 'pack' must be a struct or class
- >name in function Pack::Pack()
- >Error PROJECT.CPP 72: Statement missing ; in function Pack::Pack()
- >
- >The Borland manuals are not much help, all the examples are too
- >simple.
- >
- >Is there any one out there who can point in the correct direction?
- >
- >Here's the code:
- >
- >// Poker playing program v0.0
- >// by N A Llewellyn
- >
- >#include <iostream.h>
- >#include <classlib\arrays.h>
- >
- >// in order of rank from low to high
- >enum RANK
- >{two,three,four,five,six,seven,eight,nine,ten,jack,queen,king,ace};
- >
- >enum SUIT {clubs,harts,diamonds,spades}; // no order of rank
- >
- >enum FACE {up,down};
- >
- >class Card {
- >
- > private:
- > RANK rank;
- > SUIT suit;
- > FACE face;
- >
- > friend ostream& operator << (ostream&,const Card&);
- >
- > public:
- > Card() {rank=two;suit=clubs;face=down;}
- > Card(const Card & c)
- {rank=c.rank;suit=c.suit;face=c.face;}
- > int operator == (const Card & c) {return rank == c.rank;}
- > int operator < (const Card & c) {return rank < c.rank;}
- > void SetRank(RANK r) {rank=r;}
- > void SetSuit(SUIT s) {suit=s;}
- > void SetFace(FACE f) {face=f;}
- >};
- >
- >typedef TSArrayAsVector <Card> CardArray;
- >typedef TSArrayAsVectorIterator <Card> CardArrayIterator;
- >
- >class Pack {
- >
- > private:
- > CardArray pack;
- > unsigned int NumberOfCards;
- >
- > public:
- > Pack();
- > void AddCard(Card c) {pack.Add(c);NumberOfCards++;}
- > void FindAndDetach(Card c) {pack.Detach(c);}
- > void DetachFrom(int i) {pack.Detach(i);}
- > void FlushPack(void) {pack.Flush();NumberOfCards=0;}
- > int IsPackEmpty(void) {return pack.IsEmpty();}
- >};
- >
- >class Hand {
- >
- > private:
- > CardArray hand;
- > unsigned int NumberOfCards;
- >
- > public:
- > Hand();
- > void AddCard(Card c) {hand.Add(c);NumberOfCards++;}
- > void FindAndDetach(Card c) {hand.Detach(c);}
- > void DetachFrom(int i) {hand.Detach(i);}
- > void FlushPack(void) {hand.Flush();NumberOfCards=0;}
- > int IsPackEmpty(void) {return hand.IsEmpty();}
- > ~Hand();
- >};
- >
- >ostream& operator << (ostream& os,const Card& c)
- > {return os<<c.rank<<","<<c.suit<<","<<c.face;}
- >
- >// definition of constructor for Pack
- >
- >Pack::Pack()
- >{
- > Card card;
- > SUIT suit;
- > RANK rank;
- >
- > NumberOfCards=0;
- >
- > for(suit=clubs;suit<=spades;suit++)
- > for(rank=two;rank<=ace;rank++)
- > {
- > card.SetSuit(suit);
- > card.SetRank(rank);
- > card.SetFace(down);
- > AddCard(card);
- > NumberOfCards++;
- > }
- >}
- >
- >void Show(Card& c,void*)
- > {cout<<c<<endl;}
- >
- >void UseForEach(CardArray& c)
- > {c.ForEach(Show,0);}
- >
- >int main()
- >{
- > Pack pack1;
- > UseForEach(pack1.pack);
- > return 0;
- >}
- >
-
- You're running into this problem because Borland's TArrayAsVector class
- does not have a default constructor. For instance, when your Pack
- class gets constructed, one of the first things the compiler does is
- search for the constructor for each data member of the class. Since you
- typedef'd your CardArray as a TArray, the compiler will search for
- TArray's default constructor. Since it doesn't have one, you get this
- error message.
-
- The way to solve it is to include your TArray in the initialization
- list for the constructor of your Pack (and Hand) class. The code would
- look like this:
-
- ----------
- Pack::Pack() : pack(n1,n2,n3)
- {
- //your constructor code
- }
- ----------
-
- Since your data member, pack, is a CardArray, it needs to have its
- constructor specified in the initialization list, which resides after
- the single colon. The values n1, n2, and n3 are defined as:
-
- n1: Initial size of the array
- n2: Initial start point of the array (usually 0)
- n3: Number of extra array members to dynamically add if the array
- grows beyond its inital bound.
-
- Hope this helps.
-
- bob
- nanninr@ix.netcom.com
-